import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store/store.js'
const Home = () => import('@/views/home/Home.vue')
const Category = () => import('@/views/category/Category.vue')
const ShopCar = () => import('@/views/shopcart/ShopCar.vue')
const User = () => import('@/views/user/User.vue')
const ProductList = () => import('@/components/content/product/ProductList.vue')
const ProductDetail = () => import('@/components/content/product/ProductDetail.vue')
const Login = () => import('@/components/content/login/login.vue')
const ProFile = () => import('@/components/content/user/ProFile.vue')
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/home', component: Home, meta: { index: 1 } },
  { path: '/home', component: Home, meta: { index: 1 } },
  { path: '/category', component: Category, meta: { index: 1 } },
  { path: '/shopcart', component: ShopCar, meta: { index: 1, auth: true } },
  { path: '/user', component: User, meta: { index: 1, auth: true } },
  { path: '/productList/:id', component: ProductList, meta: { index: 2 } },
  { path: '/productDetail/:id', component: ProductDetail, meta: { index: 3 } },
  { path: '/login', component: Login, meta: { index: 2 } },
  { path: '/proFile', component: ProFile, meta: { index: 2 } },
  {
    path: '/proxy',
    name: 'Proxy',
    component: () => import('@/components/content/login/proxy.vue'),
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
})
router.beforeEach((to, from, next) => {
  if (to.name == 'login' && store.getters.getIsLogin) {
    next('/login')
  }
  if (to.meta.auth) {
    if (store.getters.getIsLogin) {
      next()
    } else {
      next({ path: '/login' })
    }
  } else {
    next()
  }
})
export default router
